package persistency.reader;

/**
 * CourseReader.java
 * Purpose: this class represents a course that students can take
 * 
 * @author Hans Brems
 * @version 1.0 05/11/2011
 */

import java.util.ArrayList;

import model.Course;
import model.container.CourseContainer;

public class RequiredCourseReader extends ContentReader {
	
	/** Constructs a course reader object **/
	public RequiredCourseReader() {
	}
	
	public RequiredCourseReader(String filePath) {
		super(filePath);
	}
	
	/** Reads a text file containing course objects into a collection */
	public ArrayList<Course> read() {
		
		// Create list
		ArrayList<Course> courses = new ArrayList<Course>();
		
		// Skip the first line (header)
		this.scanner.nextLine();

		// Construct a course object from each line
		while (this.scanner.hasNext()) {
			
			// Create a new course
			int courseNumber = Integer.parseInt(this.scanner.next());
			Course course = CourseContainer.getInstance().getCourseByID(courseNumber);
			int requiredCourseNumber = Integer.parseInt(this.scanner.next());
			Course requiredCourse  = CourseContainer.getInstance().getCourseByID(requiredCourseNumber);
			
			// Add required course as requirement
			try
			{
				course.addRequiredCourse(requiredCourse);
				
			}
			catch (Exception ex)
			{
				System.out.println("Cannot add required course: " + ex.getMessage());
			}
			
			// Add course to collection
			courses.add(course);
		}
		
		// Return list
		return courses;
	}
}